﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.IO;
using System.Web;
using Client.Sys.ServiceSys;
using Sub.Function;

namespace Client.Sys
{
   public partial class GetUrlService: SysClient, IDisposable
    {
        public GetUrlService()
            : base()
        {
            this.Endpoint.Address = new EndpointAddress(new Uri(Get_Service_URL() + "/Sys.svc"));
        }

        void IDisposable.Dispose()
        {
            if (this.State == CommunicationState.Faulted)
            {
                this.Abort();
            }
            else
            {
                this.Close();
            }
        }

        public static string Get_Service_URL()
        {
            if (!GetUrlService.DynamicSetup)
            {
                var file = new FileInfo(HttpContext.Current.Server.MapPath("~/config/appSettings.config"));
                if (file.LastWriteTime != ReadTime)
                {
                    URL = Associate.ConvertUrl((new System.Configuration.AppSettingsReader()).GetValue("Service", typeof(string)).ToString());
                        ReadTime = file.LastWriteTime;                    
                }
            }
            return URL;
        }

        public static string URL { get; set; }
        static DateTime ReadTime;
        /// <summary>
        /// true if set URL when call service false load service url from config
        /// </summary>
        public static bool DynamicSetup { get; set; }

    }

    
}
